<?php

class News{

	
	private $_id;
	private $_category_id;
	private $_title;
	private $_summary;
	private $_content;
	private $_published;
	private $_url_imagen;
	private $_created;	
	public $link;

	function __construct($category, $title, $summary, $content, $published, $created){	
			
			// Asignamos 
			$this->_category_id = $category;			
			$this->_title = $title;
			$this->_summary = $summary;
			$this->_content= $content;
			$this->_published = $published;
			$this->_created = $created;
	}

	

	function getNewByOrder($orden, $tipo){

		// orden en tipo 1 es el orden de la noticia
		// orden en tipo 2 es el ID de la noticia

		if($tipo==1) {
			$noticias=mysql_query("SELECT * FROM news where orden =".$orden." and is_published = 1 ORDER BY created DESC, id desc LIMIT 1");
		}
		if($tipo==2){
			$noticias=mysql_query("SELECT * FROM news where id =".$orden." and is_published = 1 ORDER BY created DESC, id desc LIMIT 1");	
		}
		
		if(mysql_num_rows($noticias) != 0){

			$rownoticias=mysql_fetch_array($noticias);			
			$noticia=null;

			$id_news = $rownoticias['id'];
			$category_id = $rownoticias['category_id'];
			$summary=limpiar_parr($rownoticias['summary']); #No se por que mierda el <p> hace salto de linea
			$summary=substr($summary,0,400);
			$title=$rownoticias['title'];
			$title=substr($title,0,140);			
			$created=$rownoticias['created'];
			$created=explode(" ", $created);
			$date=$created[0];
			$time=$created[1];

			
			$respfoto = mysql_query("select * from images_news where id_news='$id_news'");
			if(mysql_num_rows($respfoto)!=0){				
				while($rows = mysql_fetch_array($respfoto)) {
					$url=$rows['url'];
					$folder=$rows['folder'];					
				}				
			}

			// Asignamos 
			$this->_id = $id_news;
			$this->_category_id = $category_id;
			$this->_title = $title;
			$this->_summary = $summary;
			$this->_url_imagen = $folder.$id_news.'_'.$url;		
			$this->_fecha = cambiarFechaBase($date).' - '.$time;

			//funcion que me devuelve el link
			$this->link = getLink($id_news, $title);
		}

	}


	public function save(){
		$campos="user_id, category_id, title, summary, content, created, is_published";	
		$valores="'{$_SESSION['usuario']["idusuario"]}','$this->_category_id', '$this->_title', '$this->_summary','$this->_content','$this->_created',$this->_published";	
		sqlinsert("news", $campos, $valores);
	}

	public function update($id_news){		
		$valores="user_id = '{$_SESSION['usuario']["idusuario"]}',category_id = '$this->_category_id', title = '$this->_title', summary = '$this->_summary',content = '$this->_content', is_published = $this->_published";	
		$where = "id = $id_news";

		sqlupdate("news", $valores, $where);
	}



	public function getId(){
		return $this->_id;
	}

	public function getTitle(){
		return $this->_title;
	}

	public function getSummary(){
		return $this->_summary;
	}	

	public function getUrlImagen(){
		return $this->_url_imagen;
	}

	public function getCreated(){
		return $this->_created;
	}




	
	}	
